package com.boluo.web.controller.audit;

import java.io.*;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.boluo.common.annotation.Log;
import com.boluo.common.core.controller.BaseController;
import com.boluo.common.core.domain.AjaxResult;
import com.boluo.common.enums.BusinessType;
import com.boluo.audit.domain.AudBpmnInfo;
import com.boluo.audit.service.IAudBpmnInfoService;
import com.boluo.common.utils.poi.ExcelUtil;
import com.boluo.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 流程定义明细Controller
 * 
 * @author boluo
 * @date 2023-04-16
 */
@RestController
@RequestMapping("/audit/bpmnInfo")
public class AudBpmnInfoController extends BaseController
{
    @Autowired
    private IAudBpmnInfoService audBpmnInfoService;

    /**
     * 查询流程定义明细列表
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(AudBpmnInfo audBpmnInfo)
    {
        startPage();
        List<AudBpmnInfo> list = audBpmnInfoService.selectAudBpmnInfoList(audBpmnInfo);
        return getDataTable(list);
    }

    /**
     * 导出流程定义明细列表
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:export')")
    @Log(title = "流程定义明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AudBpmnInfo audBpmnInfo)
    {
        List<AudBpmnInfo> list = audBpmnInfoService.selectAudBpmnInfoList(audBpmnInfo);
        ExcelUtil<AudBpmnInfo> util = new ExcelUtil<AudBpmnInfo>(AudBpmnInfo.class);
        util.exportExcel(response, list, "流程定义明细数据");
    }

    /**
     * 获取流程定义明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(audBpmnInfoService.selectAudBpmnInfoById(id));
    }

    /**
     * 新增流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:add')")
    @Log(title = "流程定义明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AudBpmnInfo audBpmnInfo)
    {
        return toAjax(audBpmnInfoService.insertAudBpmnInfo(audBpmnInfo));
    }

    /**
     * 新增流程定义明细
     *
     * file: (二进制)
     * bpmnLabel: 1.png
     * bpmnType: null
     * info: null
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:add')")
    @Log(title = "流程定义明细", businessType = BusinessType.INSERT)
    @PostMapping("/flow")
    public AjaxResult flow(AudBpmnInfo audBpmnInfo, MultipartFile file) throws IOException {

        return toAjax(audBpmnInfoService.saveBpmnInfo(audBpmnInfo,file.getInputStream()));
    }

    /**
     * 修改流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:edit')")
    @Log(title = "流程定义明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AudBpmnInfo audBpmnInfo)
    {
        return toAjax(audBpmnInfoService.updateAudBpmnInfo(audBpmnInfo));
    }

    /**
     * 删除流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:remove')")
    @Log(title = "流程定义明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(audBpmnInfoService.deleteAudBpmnInfoById(id));
    }

    /**
     * 查询流程信息图和xml
     * 响应文件内容
     * 方法写 void 由我们开发者来自定义响应内容
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:queryProcess')")
    @GetMapping(value = "/queryProcess/{id}/{type}")
    public void queryProcess(@PathVariable("id") Long id,@PathVariable("type") String type,HttpServletResponse response) throws IOException {


        // 把文件内容或者图拿搭配封装一个 InputStream对象
        InputStream in = audBpmnInfoService.getInputStream(id,type);


        // response 写出去
        FileCopyUtils.copy(in,response.getOutputStream());
    }


}
